package com.edu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.demo.entity.Teacher;
import com.edu.demo.mapper.TeacherMapper;
import com.edu.demo.pojo.AjaxResult;
import com.edu.demo.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    public List<Teacher> queryByPage(int start, int size, AjaxResult result){
        IPage<Teacher> page = new Page<>(start,size);
        IPage<Teacher> iPage = teacherMapper.selectPage(page, new QueryWrapper<>());
        List<Teacher> teachers = iPage.getRecords();
        teachers.forEach(teacher -> teacher.setPassword(""));
        long total = iPage.getTotal();
        result.put("total",total);
        return teachers;
    }


    public int updateTeacher(Teacher teacher){
        int update = teacherMapper.updateById(teacher);
        return update;
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateTeachers(List<Teacher> teachers){
        teachers.forEach(teacher -> teacherMapper.updateById(teacher));
    }

    @Override
    public int insertTeacher(Teacher teacher) {
        int insert = teacherMapper.insert(teacher);
        return insert;
    }

    /**
     * 按条件查询教师
     *
     * @param teacher
     * @return
     */
    @Override
    public List<Teacher> findTeachers(Teacher teacher,int index,int size,AjaxResult ajaxResult) {

        IPage<Teacher> page = new Page<>(index,size);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if(teacher.getId()!=null)
            queryWrapper.eq("id",teacher.getId());
        if(teacher.getCode()!=null&&!teacher.getCode().equals(""))
            queryWrapper.eq("code",teacher.getCode());
        if(Strings.isNotBlank(teacher.getName()))
            queryWrapper.likeLeft("name",teacher.getName());
        if(Strings.isNotBlank(teacher.getEmail()))
            queryWrapper.likeLeft("email",teacher.getEmail());
        if(Strings.isNotBlank(teacher.getDeptName()))
            queryWrapper.likeLeft("dept_name",teacher.getDeptName());
        if(teacher.getStatus()!=null)
            queryWrapper.eq("status",teacher.getStatus());

        if(teacher.getDeptId()!=null)
            queryWrapper.eq("dept_id",teacher.getDeptId());

        IPage<Teacher> iPage = teacherMapper.selectPage(page, queryWrapper);
        List<Teacher> teachers = iPage.getRecords();
        ajaxResult.put("total",iPage.getTotal());
        teachers.forEach(teacher1 -> teacher1.setPassword(null));
        return teachers;
    }


}
